package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

// @RestController
@Controller
public class TestController {
    // Response对象实现请求重定向
    @RequestMapping("/rdt")
    public void myDirect2(HttpServletResponse response) throws IOException {
        response.sendRedirect("/calc.html");
    }

    // 实现请求重定向
    @RequestMapping("/rd")
    public String myDirect() {
        return "redirect:/login.html";
    }

    // 使用HttpServlet实现请求转发
    @RequestMapping("/fwd")
    public void myForward2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/calc.html").forward(request,response);
    }

    @RequestMapping("/fw")
    public String myForward() {
        return "/calc.html";
        //return "forward:/calc.html";
    }

    @RequestMapping("/test")
    public String test() {
        return "this is test2.";
    }
}
